package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PriceAndNumber;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 查询今日运营数据
     * */
    @Override
    public BusinessDataVO businessData() {
        LocalDate localDate = LocalDate.now();
        //营业额
        List<PriceAndNumber> priceAndNumberList = reportMapper.getAmount(localDate, localDate.plusDays(1), Orders.COMPLETED);
        BigDecimal amount = BigDecimal.valueOf(0);
        for (PriceAndNumber priceAndNumber : priceAndNumberList) {
            amount = amount.add(priceAndNumber.getAmount());
        }
        Double account = amount.doubleValue();

        //有效订单数
        Integer validSum = reportMapper.getOrderSum(Orders.COMPLETED, localDate, localDate.plusDays(1));

        //订单完成率
        Integer sum = reportMapper.getOrderSum(null, localDate, localDate.plusDays(1));
        double rate = (double) validSum/(double) sum;

        //平均客单价 客单价=销售额÷成交顾客数
        Integer userAccount = orderMapper.getUserAccountByStatusAndDate(Orders.COMPLETED, localDate, localDate.plusDays(1));
        Double unitPrice = account/(double)userAccount;

        //新增用户数
        Integer newUser = reportMapper.getNewUser(localDate, localDate.plusDays(1));

        BusinessDataVO businessDataVO = new BusinessDataVO(account,validSum,rate,unitPrice,newUser);
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * */
    @Override
    public OrderOverViewVO overviewOrders() {
        LocalDate localDate = LocalDate.now();
        //全部订单
        Integer sum = reportMapper.getOrderSum(null, localDate, localDate.plusDays(1));

        //已取消数量
        Integer cancelledSum = reportMapper.getOrderSum(Orders.CANCELLED, localDate, localDate.plusDays(1));

        //已完成数量
        Integer completedSum = reportMapper.getOrderSum(Orders.COMPLETED, localDate, localDate.plusDays(1));

        //待派送数量
        Integer deliveredSum = reportMapper.getOrderSum(Orders.CONFIRMED, localDate, localDate.plusDays(1));

        //待接单数量
        Integer waitingSum = reportMapper.getOrderSum(Orders.TO_BE_CONFIRMED, localDate, localDate.plusDays(1));

        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(sum);
        orderOverViewVO.setCancelledOrders(cancelledSum);
        orderOverViewVO.setCompletedOrders(completedSum);
        orderOverViewVO.setDeliveredOrders(deliveredSum);
        orderOverViewVO.setWaitingOrders(waitingSum);
        return orderOverViewVO;
    }

    /**
     * 查询套餐总览
     * */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
        //已启售套餐数量
        Integer enableSum = setmealMapper.getStemealSumByStatus(StatusConstant.ENABLE);
        //已停售套餐数量
        Integer disableSum = setmealMapper.getStemealSumByStatus(StatusConstant.DISABLE);
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO(enableSum,disableSum);
        return setmealOverViewVO;
    }

    /**
     * 查询菜品总览
     * */
    @Override
    public DishOverViewVO overviewDishes() {
        Integer enableSum = dishMapper.getDishesByStatus(StatusConstant.ENABLE);
        Integer disableSum = dishMapper.getDishesByStatus(StatusConstant.DISABLE);
        DishOverViewVO dishOverViewVO = new DishOverViewVO(enableSum,disableSum);
        return dishOverViewVO;
    }

}
